package com.jtf.web.link.application;

import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.w3c.dom.Document;

import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class  BaseApplication extends Stage {

    protected static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

    protected boolean testModel = false;

    protected WebEngine engine;

    public BaseApplication(String url, String ua){

        setWidth(Screen.getPrimary().getVisualBounds().getWidth());
        setHeight( Screen.getPrimary().getVisualBounds().getHeight());
        WebView webView = new WebView();
        close();
        StackPane root = new StackPane();
        engine = webView.getEngine();
        engine.setUserAgent(ua);
        engine.load(url);
        root.getChildren().add(webView);
        Scene scene = new Scene(root);
        setScene(scene);
        new Thread(){
            @Override
            public void run() {
                super.run();

                while (!isShowing()){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                }
                Document document = engine.getDocument();
                while (document == null){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                    document = engine.getDocument();

                }
                webLoad();

            }
        }.start();
    }
    public void test(){
        testModel = true;
        show();
    }

    protected void webLoad(){
        show();
        System.out.println("加载完成");
    }

}
